package com.itheima.manager.contrller;

import com.itheima.manager.domain.Subway;
import com.itheima.manager.service.SubwayService;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

public class SubwayController {
    static Scanner sc = new Scanner(System.in);
    static SubwayService service = new SubwayService();

    public static void main(String[] args) {
        System.out.println("==========================欢迎您来到北京地铁管理系统==========================");
        login();
        while (true) {
            System.out.println("请输入您的选择:  1.查看地铁站  2.增加地铁站  3.修改地铁站  4.删除地铁站  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    selectSubways();
                    break;
                case 2:
                    addSubway();
                    break;
                case 3:
                    updateSubway();
                    break;
                case 4:
                    deleteSubway();
                    break;
                case 5:
                    System.out.println("感谢您的使用,下次再见!");
                    System.exit(0);
                default:
                    System.out.println("您输入有误,请重新输入....");
            }
        }
    }

    private static void updateSubway() {
        String id=inputIdGetId(true);
        Subway sub=inputInfoSubway(id);
        if(service.updateSubway(id,sub)){
            System.out.println("==============修改成功==============");
        }else{
            System.out.println("==============修改失败==============");
        }

    }

    private static void deleteSubway() {
        String id=inputIdGetId(true);
        if(service.deleteSubway(id)){
            System.out.println("==============删除成功==============");
        }else{
            System.out.println("==============删除失败==============");
        }

    }

    private static void addSubway() {
        String id = inputIdGetId(false);
        Subway sub = inputInfoSubway(id);
        if (service.addSubway(sub)) {
            System.out.println("==============添加成功==============");
        } else {
            System.out.println("==============添加失败==============");
        }
    }

    private static Subway inputInfoSubway(String id) {
        System.out.println("请输地铁名称:");
        String name = sc.next();
        System.out.println("请输地铁所属路线:");
        String line = sc.next();
        Subway sub = new Subway(id, name, line);
        return sub;

    }

    private static String inputIdGetId(boolean flag) {
        String id;
        System.out.println("请输入id:");
        while (true) {
            id = sc.next();
            boolean isExist = service.isExist(id);
            if (flag == isExist) {
                break;
            } else {
                System.out.println("请重新输入id;");
            }
        }
        return id;
    }

    private static void selectSubways() {
        ArrayList<Subway> list = service.selectSubways();
        if (list == null) {
            System.out.println("没有查找到信息.请添加后查询!");
        } else {
            System.out.println("地铁编号    地铁名称    地铁所属线路");
            list.stream().filter(sub -> sub != null).forEach(sub -> System.out.println(sub.show()));
        }


    }

    private static void login() {
        for (int i = 1; i <= 3; i++) {
            try {
                Properties prop = new Properties();
                BufferedReader br = new BufferedReader(new FileReader("地铁系统案例二\\prop.properties"));
                prop.load(br);
                System.out.println("请输入工号:");
                String id = sc.next();
                System.out.println("请输入姓名:");
                String name = sc.next();
                String property = prop.getProperty(id);
                if (property != null && name.equals(property)) {
                    System.out.println("==============================登录成功==============================");
                    break;
                } else {
                    if (i == 3) {
                        System.out.println("您今天没有登陆机会了...");
                        System.exit(0);
                    } else {
                        System.out.println("您今天还有" + (3 - i) + "次机会");
                        System.out.println("==============================登录失败==============================");

                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
